﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Action.MetaData.Entity;
using T.FAS.Authority.MetaData.Entity;
using T.FAS.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.MetaData.Entity;
using T.FAS.MetaData.RTP.Client;
using T.FAS.MetaData.SPI;
using T.FAS.Runtime.Metadata.Entity.Page;

namespace T.FAS.Runtime.Metadata.Plugin
{
    /// <summary>
    /// Page元数据扩展管理器
    /// </summary>
    public class PageExtRTPManager : IExtMetaDataManager
    {
        public const string path = "/telddoc/141/";
        public IMetaData GetMetaDataExt(IMetaData metaData)
        {
            var page = metaData.Content as PageMetaData;
            if(page == null)
                throw new FasException(PageExceptionCode.GetFailed, "Page元数据扩展失败，metaData不能为空");
            var instance = MetaDataClientService.GetInstance();
            //主题
            var theme = page.Header.Theme;
            if (!string.IsNullOrEmpty(theme?.ID))
            {
                ThemeMetaData themeMetaData = instance.GetMetaDataContent(theme.ID) as ThemeMetaData;
                if (themeMetaData == null)
                    throw new FasException(PageExceptionCode.GetFailed, "获取主题元数据失败:" + theme.ID);
                string CssName = themeMetaData.Code;
                string[] notCodes = new string[5] { "_", "+", "&", "%", "/" };
                foreach (var notCode in notCodes)
                    CssName = CssName.Replace(notCode, "");
                page.Header.ThemeURL = path + CssName + ".css";
            }
            if (!string.IsNullOrEmpty(page.Header.BPOID))
                page.Header.BPO = instance.GetMetaDataContent(page.Header.BPOID) as BizPermissionObject;
            else
                page.Header.BPO = new BizPermissionObject();
            foreach (var item in page.ViewModels)
            {
                if(item.ActionType == Entity.ActionType.ClientAction)
                {
                    var clientAction = instance.GetMetaDataContent(item.ActionID) as ActionMetaData;
                    item.JSScript = clientAction?.Method?.MethodContent;
                }
            }
            var dic = new Dictionary<string, string>();
            dic.Add("DefaultPCTheme", path + "DefaultPCTheme.css");
            dic.Add("DefaultMobileTheme", path + "DefaultMobileTheme.css");
            dic.Add("DefaultReactiveTheme", path + "DefaultReactiveTheme.css");
            dic.Add("PCComponent", path + "PCComponent.css");
            dic.Add("MobileComponent", path + "MobileComponent.css");
            page.Header.DefaultThemeURL = dic;
            metaData.Content = page;
            return metaData;
        }

        public void MetaChangeNotify(IMetaData metaData, string changeType)
        {

        }

        public void RepairMetaData(string metaDataId)
        {

        }

        public void CreateBranchMetaData(string sourceProjectBranchID, string newProjectBranchID, string versionID, string newVersionID)
        {

        }
    }
}
